/*
 * Copyright 2016 drakeet. https://github.com/drakeet
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.epfresh.adapter;

import android.content.Intent;
import android.net.Uri;
import android.support.annotation.NonNull;
import android.support.v4.view.ViewPager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.epfresh.R;
import com.epfresh.activity.CommonStoreActivity;
import com.epfresh.activity.ListStoreActivity;
import com.epfresh.activity.WebActivity;
import com.epfresh.api.entity.User;
import com.epfresh.api.helper.ApplicationHelper;
import com.epfresh.api.utils.AppUtils;
import com.epfresh.api.utils.LocalDisplay;
import com.epfresh.bean.Ad;
import com.epfresh.bean.HomeTop;
import com.epfresh.bean.*;
import com.epfresh.constant.Constant;
import com.epfresh.views.banner.*;
import com.epfresh.views.banner.SliderBannerController;
import com.umeng.analytics.MobclickAgent;

import multitype.ItemViewBinder;


public class ViewBinderHomeBanner
    extends ItemViewBinder<HomeTopBean, ViewBinderHomeBanner.ViewHolder> implements View.OnClickListener{

    @NonNull @Override
    protected ViewHolder onCreateViewHolder(
        @NonNull LayoutInflater inflater, @NonNull ViewGroup parent) {
        View view = inflater.inflate(R.layout.ad_slider_banner, parent, false);
        return new ViewHolder(view);
    }


    @Override
    public void onClick(View v) {
        Object tag = v.getTag(R.id.item_key_status);
        if (tag != null && tag instanceof Ad) {
            Ad ad = (Ad) tag;
            if (ad.getLink()!=null&&ad.getLink().contains("num=1706001")) {//"red_packet.html?num=1706001")){
                Intent redBag = new Intent(v.getContext(), WebActivity.class);
                User user = ApplicationHelper.getInstance().getUser();
                if(user!=null&&user.getAccess_token()!=null){
                    redBag.putExtra(Constant.KEY_WEB_URL, ad.getLink()+"&phone="+user.getPhone()+"&token="+user.getAccess_token()+"&cid=" + AppUtils.getUniquePsuedoID());
                    Log.e("top after login",ad.getLink()+"&phone="+user.getPhone()+"&token="+user.getAccess_token()+"&cid=" + AppUtils.getUniquePsuedoID());
                }
                else{
                    redBag.putExtra(Constant.KEY_WEB_URL, ad.getLink()+"&phone=unlogin");
                    Log.e("top unlogin",ad.getLink()+"&phone=unlogintst");
                }
                redBag.putExtra(Constant.KEY_WEB_TITLE, ad.getTitle());
                redBag.putExtra("share_web", ad.getLink());
                redBag.putExtra("image_url", ad.getImg());
                redBag.putExtra("share_type", 1);
                redBag.putExtra("share_content", ad.getContent());
                MobclickAgent.onEvent(v.getContext(), "Click_Home_Banner");/*统计广告点击*/
                Log.e("redbagDialog:","KEY_WEB_URL:"+redBag.getStringExtra(Constant.KEY_WEB_URL)+"\nshare_web:"+redBag.getStringExtra("share_web")+"\nshare_content:"+redBag.getStringExtra("share_content")+"");
                v.getContext().startActivity(redBag);
                return;
            }
            Intent intent = new Intent(v.getContext(), WebActivity.class);
            intent.putExtra(Constant.KEY_WEB_URL, ad.getLink());
            intent.putExtra(Constant.KEY_WEB_TITLE, ad.getTitle());
            if(ad.getLink().contains("_share_")) {
                intent.putExtra("share_web", ad.getLink());
                intent.putExtra("share_type", 1);
                intent.putExtra("share_content", ad.getContent());
                intent.putExtra("image_url", ad.getImg());
            }
            intent.putExtra("image_url", ad.getImg());
            v.getContext().startActivity(intent);
        }
    }
    @Override
    protected void onBindViewHolder(@NonNull ViewHolder holder, @NonNull HomeTopBean category) {

        SliderBannerController sliderBannerController = new SliderBannerController(holder.vp.getContext(),
                holder.vp);
        int w = LocalDisplay.SCREEN_WIDTH_PIXELS;
        int h = w * 405 / 1080;
        ViewGroup.LayoutParams lp = new ViewGroup.LayoutParams(w, h);
        holder.itemView.setLayoutParams(lp);
        holder.itemView.setBackgroundResource(R.drawable.def_img);
        sliderBannerController.setmClickItemListener(this);
        sliderBannerController.play(category.getBanners());
        View v = holder.vp.findViewById(R.id.slider_banner_indicator);
        v.setVisibility(View.VISIBLE);
    }


    static class ViewHolder extends RecyclerView.ViewHolder {
        SliderBanner vp;
        ViewHolder(@NonNull View itemView) {
            super(itemView);
            vp = (SliderBanner) itemView.findViewById(R.id.vp);
        }
    }
}
